/*******************************************************************************
 * Copyright (c) 2000, 2008 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *     Eclipse Foundation - Modified from org.eclipse.jdt.ui.actions.OpenNewClassWizardAction
 *******************************************************************************/
package org.schemeway.plugins.schemescript.wizards;

import org.eclipse.core.resources.IWorkspaceRoot;
import org.eclipse.core.resources.ResourcesPlugin;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.jdt.internal.ui.JavaPluginImages;
import org.eclipse.jdt.internal.ui.wizards.NewWizardMessages;
import org.eclipse.jdt.ui.actions.AbstractOpenWizardAction;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.jface.resource.*;
import org.eclipse.jface.viewers.IStructuredSelection;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.ui.INewWizard;
import org.schemeway.plugins.schemescript.*;

public class OpenNewSchemeFileWizardAction extends AbstractOpenWizardAction {
	
	private NewSchemeFileWizardPage fPage;
	private boolean fOpenEditorOnFinish;
	private IStructuredSelection selection;
	private static ImageDescriptor mSchemeFileIcon = SchemeScriptImageRegistry
	.getImageDescriptor("icons/scheme_new.gif");
	/**
	 * Creates an instance of the <code>OpenNewClassWizardAction</code>.
	 */
	public OpenNewSchemeFileWizardAction() {
		setText("New Scheme File"); 
		setDescription("Create a new Scheme File."); 
		setToolTipText("New Scheme File"); 
		//setImageDescriptor(JavaPluginImages.DESC_WIZBAN_NEWCLASS);
		 
		setImageDescriptor(mSchemeFileIcon);
		//PlatformUI.getWorkbench().getHelpSystem().setHelp(this, IJavaHelpContextIds.OPEN_CLASS_WIZARD_ACTION);
		
		fPage= new NewSchemeFileWizardPage("New Scheme File Wizard", selection);
		fOpenEditorOnFinish= true;
	}
	
	/**
	 * Sets a page to be used by the wizard or <code>null</code> to use a page initialized with values
	 * from the current selection (see {@link #getSelection()} and {@link #setSelection(IStructuredSelection)}).
	 * @param page the page to use or <code>null</code>
	 */
	public void setConfiguredWizardPage(NewSchemeFileWizardPage page) {
		fPage= page;
	}
	
	/**
	 * Specifies if the wizard will open the created type with the default editor. The default behaviour is to open
	 * an editor.
	 * 
	 * @param openEditorOnFinish if set, the wizard will open the created type with the default editor
	 * 
	 * @since 3.3
	 */
	public void setOpenEditorOnFinish(boolean openEditorOnFinish) {
		fOpenEditorOnFinish= openEditorOnFinish;
	}
	
	/* (non-Javadoc)
	 * @see org.eclipse.jdt.ui.actions.AbstractOpenWizardAction#createWizard()
	 */
	protected final INewWizard createWizard() throws CoreException {
		return new NewSchemeFileWizard();
	}
	
	protected boolean doCreateProjectFirstOnEmptyWorkspace(Shell shell) {
		//return super.doCreateProjectFirstOnEmptyWorkspace(shell);
		IWorkspaceRoot workspaceRoot= ResourcesPlugin.getWorkspace().getRoot();
		if (workspaceRoot.getProjects().length == 0) {
			String title= NewWizardMessages.AbstractOpenWizardAction_noproject_title; 
			String message= NewWizardMessages.AbstractOpenWizardAction_noproject_message; 
			if (MessageDialog.openQuestion(shell, title, message)) {
				new NewEmptyProjectWizardAction().run();
				return workspaceRoot.getProjects().length != 0;
			}
			return false;
		}
		return true;
	}
}

